/**************************************************************************
 //* W Corp. 
 //* W Profile Services                                             
 //* Package:  by.doubleyou.progile
 //* (C) Copyright W Corp.  2007
 /**************************************************************************
 *
 * Copyright (c) 2007, W Corporation
 * All rights reserved.
 * 
 * Version: 1.0
 * Date:  28 Nov, 2007, Author: Alexander J. Perez Tchernov
 * 
 * MODIFICATION HISTORY:
 * Defect/Feature  	Description
 * --------------  	--------------------------------------------------------------
 */
package by.doubleyou.profile;

import java.io.File;
import java.io.FilenameFilter;
import java.net.MalformedURLException;

import org.restlet.Application;
import org.restlet.Component;
import org.restlet.Directory;
import org.restlet.Restlet;
import org.restlet.Router;
import org.restlet.data.MediaType;
import org.restlet.data.Protocol;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.data.Status;
import org.restlet.resource.FileRepresentation;

/**
 * 
 * @author Alexander J. Perez Tchernov
 * 
 */
public class ApplicationServer {

	private Component component;

	public ApplicationServer() throws MalformedURLException {
		component = new Component();
		component.getServers().add(Protocol.HTTP, 8080);
		component.getClients().add(Protocol.FILE);
		 
		final File pageDirectory = new File("src/main/resources");
		final String cssName = new File("src/main/resources/css").toURI().toURL().toString();
		final String jsName = new File("src/main/resources/js").toURI().toURL().toString();
		final String jpgName = new File("src/main/resources/images").toURI().toURL().toString();
		final String schemaName = new File("src/main/resources/schemas").toURI().toURL().toString();
		
		Application application = new Application(component.getContext()) {
			@Override
			public Restlet createRoot() {
				Router router = new Router(getContext());
				 
				// serve static files  //TODO cache
				router.attach("/css", new Directory(getContext(), cssName));
				router.attach("/js", new Directory(getContext(), jsName));
				router.attach("/images", new Directory(getContext(), jpgName));
				router.attach("/schemas", new Directory(getContext(), schemaName));
				
				router.attach("/profiles/{name}", new Restlet(getContext()) {
					@Override
					public void handle(Request arg0, Response arg1) {
						//TODO static 
						arg1.setEntity(new FileRepresentation("src/main/resources/onlineprofile.html",
								MediaType.TEXT_HTML, 10 /* expiration TODO etag*/));
						arg1.setStatus(Status.SUCCESS_ACCEPTED);
					}
				});
				router.attach("/profiles/{name}/{fragment}", new Restlet(getContext()){
					@Override
					public void handle(Request request, Response responce) {
						
					final String fragmentName = request.getAttributes().get("fragment").toString();
					// TODO security hook 
					 File[] files = pageDirectory.listFiles(new FilenameFilter(){
							public boolean accept(File file, String name) {			
								//System.out.print(name + " ");
								return (name.equals(fragmentName))?true:false;
							}
						});
					 System.out.println("Fragment " + fragmentName);
					 
					 if (files.length ==0) 
						 responce.setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
					 else if (files.length !=1)
						 responce.setStatus(Status.SERVER_ERROR_INTERNAL);
					 else {
						 responce.setEntity(new FileRepresentation("src/main/resources/" + fragmentName ,
									MediaType.TEXT_HTML, 10 /* expiration TODO etag*/));
						 responce.setStatus(Status.SUCCESS_ACCEPTED);
					 }
						 
						 
						 
					 
					}
				});
				
				 
				return router;
			}
		};
		component.getDefaultHost().attach(application);
	}

	public void start() throws Exception {
		component.start();
	}

	public static void main(String... strings) {
		try {
			new ApplicationServer().start();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}